<template>
	<view>
		<view id="navbar">
			<NavbarHead :toTop="false" leftTitle="分类" bgColor="rgba(0,0,0,0)"></NavbarHead>
		</view>
		<view class="topBox" id="topBox" :style="{'padding-top':topHeadHeight+ 'rpx' }">
			<view class="searchBox">
				<view class="search" @click="toUrl('/subPages/search/search')">
					<image src="http://qiniu.suboshimuying.cn/static/search/search.png" style="margin-right: 10rpx;"
						mode=""></image>
					<input class="ipt" type="text" placeholder="搜索" disabled>
				</view>
			</view>
		</view>
		<!-- 一级分类 -->
		<view class="tabsBox" id="tabsBox">
			<u-tabs :list="tabsList" :current="tabsActive" @click="tabsClick" lineWidth="30" lineColor="#7C67FF"
				:activeStyle="{
            color: '#7C67FF',
            fontWeight: 'bold',
            transform: 'scale(1.05)'
        }" :inactiveStyle="{
            color: '#93969e',
            transform: 'scale(1)'
        }" itemStyle="padding-left: 13px; padding-right: 13px; height: 40px;"></u-tabs>
		</view>

		<!-- 二级分类 -->
		<view class="twoClassify" id="twoClassify">
			<view class="twoClassify-item" v-for="(item,index) in twoDataList" :key="item.id"
				@click="twoIdClick(item.id,index)">
				<template v-if="twoID===item.id">
					<image :src="item.select_image" mode=""></image>
				</template>
				<template v-else>
					<image :src="item.image" mode=""></image>
				</template>

				<view class="name">
					{{item.name}}
				</view>
			</view>
		</view>

		<!-- 三级分类 -->
		<view class="scroll" :style="{height:scrollViewHeight+'px'}">
			<scroll-view scroll-y class="scroll-left">
				<view class="leftItem u-line-1" v-for="item in threeDataList" :key="item.id"
					:class="{'leftActive':threeID===item.id}" @click="threeIdClick(item.id)">
					{{item.name}}
				</view>
			</scroll-view>
			<scroll-view scroll-y class="scroll-right" @scrolltolower="scrolltolower" lower-threshold="100">
				<view class="goodItem" v-for="item in goodsList" :key="item.id"
					@click="toUrl(`/subPages/goodDetails/goodDetails?type=${oneID}&id=${item.id}`)">
					<image :src="item.thum_image" mode=""></image>
					<view class="goodItem-right">
						<view class="name u-line-2">
							{{item.name}}
						</view>
						<view class="footer">
							<view>
								<text style="font-size: 22rpx;">¥</text>
								<text>{{item.price}}</text>
							</view>
							<template v-if="oneID!==1">
								<view class="btn">
									立即购买
								</view>
							</template>
							<template v-else>
								<view class="btn2">
									<view class="btn2-left" @click="addCart(item.id)">
										<image src="http://qiniu.suboshimuying.cn/static/home/ic-shopping-cart@2x.png"
											mode="">
									</view>
									</image>
									<view class="btn2-right"
										@click="toUrl(`/subPages/goodDetails/goodDetails?type=${oneID}&id=${item.id}`)">
										购买
									</view>
								</view>
							</template>

						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import NavbarHead from '../../components/NavbarHead/NavbarHead.vue'
	export default {
		data() {
			return {
				topHeadHeight: 172, //顶部导航栏高度	
				scrollViewHeight: 172, //三级分类盒子高度
				// 一级分类列表
				tabsList: [{
					name: '热卖聚划算商品 ',
					id: 1
				}, {
					name: '本地服务',
					id: 2
				}, {
					name: '薅羊毛',
					id: 3
				}, {
					name: '镇店之宝',
					id: 4
				}, ],
				twoDataList: [], //二级分类列表
				threeDataList: [], //三级分类列表
				goodsList: [], //商品列表
				// tabsActive: 0, //tabs选中(索引) 一级分类索引
				oneID: 1, //一级分类id 
				twoID: 1, //二级分类id
				threeID: 1, //三级分类id
				last_page: null, //总页数
				page: 1, //当前页数
			};
		},
		computed: {
			tabsActive() {
				return this.tabsList.findIndex(item => item.id === this.oneID)
			}
		},
		onReady() {
			this.commentHeight()
		},
		onLoad() {
			// this.oneID = getApp().globalData.activeId
			this.getcategory(this.oneID)
		},
		onShow() {
			if (getApp().globalData.activeId !== this.oneID) {
				this.oneID = getApp().globalData.activeId
				this.getcategory(this.oneID)
			}

			// 1:热卖 2:本地服务 3:薅羊毛 4:镇店之宝
		},
		components: {
			NavbarHead
		},
		methods: {
			// 获取分类列表
			async getcategory(cate) {
				let res = await this.$api.getcategory({
					cate
				})
				this.twoDataList = res.data
				this.twoID = res.data[0].id
				this.threeDataList = res.data[0].three_cate
				this.threeID = this.threeDataList[0]?.id
				this.threeIdClick(this.threeID)
				// console.log(res);
			},
			// 一级分类点击
			tabsClick(e) {
				getApp().globalData.activeId = e.id
				this.oneID = e.id
				this.getcategory(e.id)
			},
			// 二级分类点击
			twoIdClick(id, index) {
				this.twoID = id
				this.threeDataList = this.twoDataList[index].three_cate
				this.threeID = this.threeDataList[0]?.id
				this.page = 1
				this.threeIdClick(this.threeID)
			},
			//三级分类点击
			async threeIdClick(id) {
				// console.log(id);
				this.threeID = id
				let res = await this.$api.getcategoods({
					cate_id: id, //三级分类ID
					page: 1, //页数
					total: 6 //条数
				})
				this.goodsList = res.data?.data
				this.last_page = res.data.last_page
				// console.log(res);
				// console.log('一级ID', this.oneID);
				// console.log('二级ID', this.twoID);
				// console.log('三级ID', this.threeID);
			},
			// 跳转
			toUrl(url) {
				uni.navigateTo({
					url
				})
			},
			// 加入购物车
			async addCart(goods_id) {
				let {
					code,
					msg
				} = await this.$api.addCart({
					goods_id,
					num: 1
				})
				this.$u.toast(msg)
				// console.log();
			},
			// 三级分类区域滚动
			async scrolltolower() {
				// console.log('123');
				if (this.page < this.last_page) {
					this.page++
					let res = await this.$api.getcategoods({
						cate_id: this.threeID, //三级分类ID
						page: this.page, //页数
						total: 6 //条数
					})
					this.goodsList.push(...res.data?.data)
				} else {
					this.$u.toast('已经到底了！')
				}
			},
			// 计算高度
			commentHeight() {
				// 计算顶部导航栏高度
				this.topHeadHeight = uni.getStorageSync('headHeight') * 2 || 172
				//获取手机屏幕可用高度
				let screenHeight = uni.getSystemInfoSync().windowHeight
				// 通过query 获取其余盒子的高度
				let query = uni.createSelectorQuery().in(this)
				//计算navbar高度
				query.select('#navbar').boundingClientRect()
				query.select('#topBox').boundingClientRect()
				query.select('#tabsBox').boundingClientRect()
				query.select('#twoClassify').boundingClientRect()
				query.exec(res => {
					let navbarHeight = res[0].height
					let topBoxHeight = res[1].height
					let tabsBoxHeight = res[2].height
					let twoClassifyHeight = res[3].height
					let scrollViewHeight = screenHeight - navbarHeight - topBoxHeight - tabsBoxHeight -
						twoClassifyHeight
					this.scrollViewHeight = scrollViewHeight
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.u-tabs__wrapper__nav {
		justify-content: space-between;
	}

	.topBox {
		box-sizing: border-box;
		padding: 0 24rpx;
	}

	.topBox::before {
		z-index: -9;
		content: "";
		position: absolute;
		width: 440rpx;
		height: 420rpx;
		border-bottom-left-radius: 100rpx;
		background-color: #D4C6FF;
		filter: blur(42px);
		right: 0;
		top: 0;
	}

	.topBox::after {
		z-index: -10;
		content: "";
		position: absolute;
		width: 400rpx;
		height: 500rpx;
		border-bottom-right-radius: 100rpx;
		background-color: #F8D5F6;
		filter: blur(26px);
		top: 0;
		left: 0;
	}

	.searchBox {
		box-sizing: border-box;
		padding: 0 24rpx;
		width: 100%;
		height: 84rpx;
		line-height: 84rpx;
		border-radius: 60rpx;
		opacity: 1;
		background: rgba(255, 255, 255, 1);
		// border: 2px solid #FFD1D6;
		border: 2px solid transparent;
		border-radius: 99px;
		background-clip: padding-box, border-box;
		background-origin: padding-box, border-box;
		background-image: linear-gradient(to right, #fff, #fff),
			linear-gradient(to right, #FFD1D6, #EECFFD);
		color: rgba(0, 0, 0, 0.8);
		font-size: 28rpx;
		font-weight: 1000;
		font-family: "PingFang SC";
		text-align: left;
		letter-spacing: 0.6rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.search {
			width: 100%;
			display: flex;
			justify-content: space-between;
			align-items: center;

			image {
				width: 32rpx;
				height: 32rpx;
				vertical-align: middle;
			}

			.ipt {
				display: inline-block;
				flex: 1;
			}
		}
	}


	.twoClassify {
		width: 100%;
		height: 180rpx;
		display: flex;
		justify-content: space-around;
		align-items: center;

		&-item {
			display: flex;
			flex-flow: column;
			justify-content: center;
			align-items: center;
			flex-wrap: wrap;

			image {
				// margin: auto;
				width: 96rpx;
				height: 96rpx;
				border-radius: 50%;
			}

			.name {
				margin-top: 7rpx;
				color: rgba(0, 0, 0, 0.8);
				font-size: 24rpx;
				font-weight: 400;
				text-align: center;
			}
		}


	}

	.scroll {
		width: 100%;
		background-color: #fff;
		display: flex;

		&-left {
			width: 22%;
			height: 100%;
			background-color: #F8F9FE;

			.leftItem {
				width: 100%;
				height: 100rpx;
				line-height: 100rpx;
				color: rgba(0, 0, 0, 0.55);
				font-size: 28rpx;
				font-weight: 400;
				text-align: center;
				box-sizing: border-box;
				padding-left: 20rpx;
			}

			.leftActive {
				background: rgba(255, 255, 255, 1);
				color: rgba(124, 103, 255, 1);
				font-size: 28rpx;
				font-weight: 500;
				position: relative;
			}

			.leftActive::after {
				content: "";
				position: absolute;
				width: 12rpx;
				height: 36rpx;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				background: radial-gradient(235.8% 163.9% at 132.1% 0%, rgba(255, 245, 252, 1) 0%, rgba(208, 132, 255, 1) 23%, rgba(121, 101, 255, 1) 56.99999999999999%, rgba(131, 111, 255, 1) 82%, rgba(130, 111, 255, 1) 100%);
				border-radius: 0 16rpx 16rpx 0;
			}
		}

		&-right {
			width: 78%;
			height: 100%;
			background-color: #fff;
			box-sizing: border-box;
			padding: 0 32rpx;

			.goodItem {
				display: flex;
				justify-content: space-between;
				margin-top: 32rpx;

				image {
					width: 180rpx;
					height: 180rpx;
					border-radius: 16rpx;
					margin-right: 10rpx;
				}

				&-right {
					flex: 1;

					.name {
						// height: 80rpx;
						color: rgba(0, 0, 0, 0.8);
						font-size: 26rpx;
						font-weight: 500;
						font-family: "PingFang SC";
					}

					.footer {
						margin-top: 50rpx;
						display: flex;
						justify-content: space-between;
						align-items: center;
						color: $priceText;
						font-size: 32rpx;
						font-weight: 500;
					}

					.btn {
						width: 144rpx;
						height: 48rpx;
						line-height: 48rpx;
						border-radius: 96rpx;
						background: rgba(128, 110, 254, 0.1);
						color: rgba(128, 110, 254, 1);
						font-size: 24rpx;
						font-weight: 400;
						text-align: center;
					}

					.btn2 {
						// width: 150rpx;
						height: 48rpx;
						border-radius: 24rpx;
						// overflow: hidden;
						// opacity: 1;
						// background: $boxBg;
						// display: flex;
						// align-items: center;
						// justify-content: space-around;


						&-left {
							background-color: $boxBg;
							display: inline-block;
							vertical-align: middle;
							height: 48rpx;
							overflow: hidden;
							border-top-left-radius: 24rpx;
							border-bottom-left-radius: 24rpx;
							padding-left: 8rpx;
							box-sizing: border-box;

							image {
								width: 48rpx;
								height: 48rpx;
							}


						}


						&-right {
							border-top-right-radius: 24rpx;
							border-bottom-right-radius: 24rpx;
							display: inline-block;
							width: 90rpx;
							height: 48rpx;
							line-height: 48rpx;
							background-color: rgba(128, 110, 254, 0.1) !important;
							text-align: center;
							color: $textColor;
							font-size: 24rpx;
						}
					}
				}
			}
		}
	}
</style>
